﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Reflection;

namespace Obra.Common
{
    public sealed class GzipWebRequest : IWebRequestCreate
    {
        public static void Register()
        {
            GzipWebRequest gzipWebRequest = new GzipWebRequest();

            var b = WebRequest.RegisterPrefix("http://", gzipWebRequest);
            var c = WebRequest.RegisterPrefix("https://", gzipWebRequest);
        }

        WebRequest IWebRequestCreate.Create(Uri uri)
        {
            HttpWebRequest httpWebRequest =
                Activator.CreateInstance(typeof(HttpWebRequest),
                BindingFlags.CreateInstance | 
                BindingFlags.Public |
                BindingFlags.NonPublic | 
                BindingFlags.Instance,
                null, 
                new object[] { uri, null }, null) as HttpWebRequest;

            if (httpWebRequest == null)
            {
                return null;
            }

            httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
            return httpWebRequest;
        }
    }
}
